package com.onewingu.gcs.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.gdata.data.DateTime;

@PersistenceCapable
public class SharedContact {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
	@Persistent
	private String saContactID;
	
	//map of userId -> contactID
	@NotPersistent
	private Map<String, String> userContactMap;
	
	@Persistent
	private List<String> userIds;
	
	@Persistent
	private List<String> userContactIds;
	
	@NotPersistent
	private DateTime updateTime;
	
	@NotPersistent
	private boolean deleteFlag;
	
	public SharedContact(String contactID, DateTime updateTime) {
		this.saContactID = contactID;
		this.updateTime = updateTime;
		this.userContactMap = new HashMap<String, String>();
		this.userIds = new ArrayList<String>();
		this.userContactIds = new ArrayList<String>();
	}
	
	public void postLoad() {
		this.userContactMap = new HashMap<String, String>();

		for(int i = 0; i < userIds.size(); i++) {
			String userId = userIds.get(i);
			String userContactId = userContactIds.get(i);
			userContactMap.put(userId, userContactId);
		}
	}
	
	public void preSave() {
		userIds = new ArrayList<String>(userContactMap.keySet());
		userContactIds = new ArrayList<String>(userContactMap.values());
	}

	/**
	 * @return the saContactID
	 */
	public String getSaContactID() {
		return saContactID;
	}

	/**
	 * @param saContactID the saContactID to set
	 */
	public void setSaContactID(String saContactID) {
		this.saContactID = saContactID;
	}

	/**
	 * @return the userContactMap
	 */
	public Map<String, String> getUserContactMap() {
		return userContactMap;
	}

	/**
	 * @param userContactMap the userContactMap to set
	 */
	public void setUserContactMap(Map<String, String> userContactMap) {
		this.userContactMap = userContactMap;
	}

	/**
	 * @return the updateTime
	 */
	public DateTime getUpdateTime() {
		return updateTime;
	}

	/**
	 * @param updateTime the contactUpdateTime to set
	 */
	public void setUpdateTime(DateTime updateTime) {
		this.updateTime = updateTime;
	}

	/**
	 * @return the deleteFlag
	 */
	public boolean isDeleteFlag() {
		return deleteFlag;
	}

	/**
	 * @param deleteFlag the deleteFlag to set
	 */
	public void setDeleteFlag(boolean deleteFlag) {
		this.deleteFlag = deleteFlag;
	}
	
	
}
